/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.thinklite.ws.cxf.client;

import cn.thinklite.ws.cxf.Constants;
import cn.thinklite.ws.cxf.service.HelloService;
import cn.thinklite.ws.cxf.vo.User;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

/**
 *
 * @author Zhaokeyong
 */
public class HelloClient {

    public static void main(String[] args) {
        try {
            System.out.println("cxf");
            cxf();
            System.out.println("xfire");
            xfire();
            System.out.println("invoke");
            invoke();
            System.exit(0);
        } catch (MalformedURLException ex) {
            Logger.getLogger(HelloClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(HelloClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void cxf() throws MalformedURLException, Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        // 注册WebService接口
        factory.setServiceClass(HelloService.class);
        // 设置WebService地址
        factory.setAddress(Constants.__URL__);
        HelloService client = (HelloService) factory.create();

        System.out.println(client.sayHello("Josen"));

        User user = new User();
        user.setName("Bill");
        user.setSex("man");
        System.out.println(client.helloUser(user));

    }

    public static void xfire() throws MalformedURLException, Exception {
        Service service = new ObjectServiceFactory().create(HelloService.class);
        XFire xfire = XFireFactory.newInstance().getXFire();
        XFireProxyFactory factory = new XFireProxyFactory(xfire);
        HelloService client = (HelloService) factory.create(service, Constants.__URL__);

        System.out.println(client.sayHello("zky"));

        User user = new User();
        user.setName("sparon");
        user.setSex("man");
        System.out.println(client.helloUser(user));
    }
    
    public static void invoke() throws MalformedURLException, Exception {
        new Client(new URL(Constants.__URL__)).invoke("sayHello", new Object[]{"test123"});
    }
}
